import deviceUtil from "../utils/deviceUtil"
import httpUtil from "../utils/httpUtil"
import logApi from "./logApi"

const setOpenId = (openId) => {
	uni.setStorageSync('openId', openId)
}

const getOpenId = () => {
	return uni.getStorageSync('openId')
}

const getMobile = () => {
	return uni.getStorageSync('mobile')
}

const setMobile = (mobile) => {
	uni.setStorageSync('mobile', mobile)
}

const setUserId = (userId) => {
	uni.setStorageSync('userId', userId)
}

const getUserId = () => {
	return uni.getStorageSync('userId')
}

const canReceiveVipList = () => {
	return [{
		title: ['腾讯视频', 'VIP视频'],
		showImg: 'https://h5.sx2b2c.com/images/nangua_shipin/lingqu/list/show-tengxun.png',
		confirmImg: 'https://h5.sx2b2c.com/images/nangua_shipin/lingqu/list/confirm-tengxun.png',
		successImg: 'https://h5.sx2b2c.com/images/nangua_shipin/lingqu/list/video-receive-success-tengxun.png',
		goodsId: '54',
	}, {
		title: ['爱奇艺视频', 'VIP视频'],
		showImg: 'https://h5.sx2b2c.com/images/nangua_shipin/lingqu/list/show-iqiyi.png',
		confirmImg: 'https://h5.sx2b2c.com/images/nangua_shipin/lingqu/list/confirm-iqiyi.png',
		successImg: 'https://h5.sx2b2c.com/images/nangua_shipin/lingqu/list/video-receive-success-iqiyi.png',
		goodsId: '30157',
	}, {
		title: ['优酷视频', 'VIP视频'],
		showImg: 'https://h5.sx2b2c.com/images/nangua_shipin/lingqu/list/show-youku.png',
		confirmImg: 'https://h5.sx2b2c.com/images/nangua_shipin/lingqu/list/confirm-youku.png',
		successImg: 'https://h5.sx2b2c.com/images/nangua_shipin/lingqu/list/video-receive-success-youku.png',
		goodsId: '84',
	}, {

		title: ['芒果视频', 'VIP视频'],
		showImg: 'https://h5.sx2b2c.com/images/nangua_shipin/lingqu/list/show-mangguo.png',
		confirmImg: 'https://h5.sx2b2c.com/images/nangua_shipin/lingqu/list/confirm-mangguo.png',
		successImg: 'https://h5.sx2b2c.com/images/nangua_shipin/lingqu/list/video-receive-success-mangguo.png',
		goodsId: '79',
	}, {
		title: ['咪咕视频', 'VIP视频'],
		showImg: 'https://h5.sx2b2c.com/images/nangua_shipin/lingqu/list/show-migu.png',
		confirmImg: 'https://h5.sx2b2c.com/images/nangua_shipin/lingqu/list/confirm-migu.png',
		successImg: 'https://h5.sx2b2c.com/images/nangua_shipin/lingqu/list/video-receive-success-migu.png',
		goodsId: '30217',
	}]
}

const loginByCode = (code, callBack, errCallBack) => {
	// 类型:1微信小程序，2支付宝H5，默认1
	let params = {
		code,
		type: 1,
	}
	// #ifdef H5
	if (deviceUtil.isAliBrowser()) {
		params.type = 2;
	}
	// #endif
	httpUtil.post('/api/ng/wxLogin', params, res => {
		logApi.addNanguaShipinLianheHuiyuan(
			'loginByCode 登录成功。请求参数：' +
			JSON.stringify(params) +
			' 。返回：' + JSON.stringify(res)
		)
		setOpenId(res.data.openid)
		if (res.data.mobile) {
			setMobile(res.data.mobile)
		} else {
			setMobile('')
		}



		if (callBack) {
			callBack(res.data)
		}
		// if (nanguaLingquApi.getMobile()) {
		// 	uni.redirectTo({
		// 		url: '/pages/nangua_lianhe_huiyuan_lingqu_list/nangua_lianhe_huiyuan_lingqu_list'
		// 	})
		// }

	}, err => {
		logApi.addNanguaShipinLianheHuiyuan(
			'wxLogin 登录发生错误。请求参数：' +
			JSON.stringify(params) +
			' 。返回：' + JSON.stringify(err)
		)
		if (errCallBack) {
			errCallBack(err)
		}
	})
}

const getAliH5Code = (callBack) => {
	ap.getAuthCode({
		appId: '2021003122645688',
		scopes: ['auth_base'],

	}, (res) => {
		if (res.authCode) {
			logApi.addNanguaShipinLianheHuiyuan('支付宝H5用户code:' + res.authCode)
			if (callBack) {
				callBack(res.authCode)
			}
		} else {
			ap.alert(JSON.stringify(res));
		}
		// callBack(res);
	});
}

const sendLoginSms = (mobile, callBack, errCallBack) => {
	let params = {
		mobile,
		event: 'login',
	}
	httpUtil.post('/api/Sms/send', params, res => {
		logApi.addNanguaShipinLianheHuiyuan('短信登录发送成功，请求参数：' +
			JSON.stringify(params) +
			'。返回：' + JSON.stringify(res))
		if (callBack) {
			callBack(res)
		}
	}, err => {
		logApi.addNanguaShipinLianheHuiyuan('短信登录发送错误，请求参数：' +
			JSON.stringify(params) +
			'。返回：' + JSON.stringify(err))
		if (errCallBack) {
			errCallBack(err)
		}
	})
}

/**
 * 类型:1微信，2支付宝，3外部浏览器 默认1
 */
const getApiType = () => {
	// #ifdef MP-WEIXIN
	return 1;
	// #endif
	// #ifdef H5
	if (deviceUtil.isAliBrowser()) {
		return 2;
	} else if (deviceUtil.isWeChatBrowser()) {
		// 这里也设置成外部浏览器这样就可以不用调整代码
		return 3;
	} else {
		return 3;
	}
	// #endif
	return 0;
}

const initReceiveVipList = (callBack) => {

	httpUtil.post('/api/ng/getGoods', {}, res => {
		callBack(res.data)
	})

}


export default {
	initReceiveVipList,
	setOpenId,
	getOpenId,
	setMobile,
	getMobile,
	setUserId,
	getUserId,
	canReceiveVipList,
	getAliH5Code,
	loginByCode,
	sendLoginSms,
	getApiType,
}